package zhoug.zui.keyboard;

import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.Editable;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import androidx.annotation.NonNull;
import zhoug.common.anim.ZAnimations;
import zhoug.logging.Logger;
import zhoug.zui.R;
import zhoug.zui.dialog.ZUIDialog;

/**
 * @Author 35574
 * @Date 2021/2/1
 * @Description
 */
public class ZUIKeyboardDialog extends ZUIDialog {
    private static final String TAG = ">>>ZUIKeyboardDialog";
    private static final boolean DEBUG=false;
    private EditText mEditText;
    private KeyboardView mKeyboardView;
    private Keyboard mKeyboard;//自定义的软键盘



    public ZUIKeyboardDialog(@NonNull Context context) {
        super(context);
    }

    public ZUIKeyboardDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }



    @Override
    protected int getLayoutResId() {
        return R.layout.zui_keyboard_dialog;
    }

    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        setOverlay(false);
        setCanceledOnTouchOutside(false);
        Window window = getWindow();
        if(window!=null){
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        }


        mKeyboardView = findViewById(R.id.keyboardView);
        if(mKeyboard==null){
            mKeyboard=new Keyboard(getContext(),R.xml.zui_lower_letter_number_key_board);
        }
        mKeyboardView.setKeyboard(mKeyboard);
        mKeyboardView.setEnabled(true);
        mKeyboardView.setPreviewEnabled(false);
        mKeyboardView.setOnKeyboardActionListener(onKeyboardActionListener);

        setAttributes(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        setWindowAnimations(ZAnimations.ANIM_DOWN);
    }

    /**
     * 软键盘监听
     */
    private KeyboardView.OnKeyboardActionListener onKeyboardActionListener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int primaryCode) {

        }

        @Override
        public void onRelease(int primaryCode) {

        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            if (mEditText == null) return;
            Editable text = mEditText.getText();
            int start = mEditText.getSelectionStart();
            if (primaryCode == Keyboard.KEYCODE_DELETE) {
                int length = text.length();
                if (length > 0 && start > 0) {
                    text.delete(start - 1, start);
                    if (DEBUG) {
                        Logger.d(TAG, "onKey:删除");
                    }
                }
            } else {
                text.insert(start, Character.toString((char) primaryCode));
            }
        }

        @Override
        public void onText(CharSequence text) {

        }

        @Override
        public void swipeLeft() {

        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeDown() {

        }

        @Override
        public void swipeUp() {

        }
    };

    public void setEditText(EditText editText) {
        this.mEditText = editText;
    }

}
